<?php
/*
 * hs_hr_employee_workgroup 员工班次表
 * hs_hr_emp_attendance "考勤记录表"
 * hs_hr_emp_attendance_sp "特殊考勤表"
 * hs_hr_workgroup 员工排班表
 * hs_hr_users 用户表
 * hs_hr_news 公告表
 * hs_hr_oamenu 自定义oa菜单表
 * hs_hr_oa_approval 自定义OA审批数据
 *
 * Power by kaysonzhang 2012/5/6
 */

require_once ROOT_PATH . '/lib/confs/Conf.php';
require_once ROOT_PATH . '/lib/confs/sysConf.php';
require_once ROOT_PATH . '/lib/dao/DMLFunctions.php';
require_once ROOT_PATH . '/lib/dao/SQLQBuilder.php';

require_once ROOT_PATH . '/lib/models/time/Timesheet.php';
require_once ROOT_PATH . '/lib/models/time/TimeEvent.php';
require_once ROOT_PATH . '/lib/models/time/ProjectReport.php';
require_once ROOT_PATH . '/lib/common/TemplateMerger.php';

class OamangeController extends Base{
	
	private $UserName;
	
	public function __construct() {
		include (ROOT_PATH . '/customConfig/attendance.config.php');
		$this->AttendanceCache = $_CfgAttendance;
		$this->db = new DMLFunctions ();
		$this->GetUserName ();
		error_reporting ( 0 );
	}
	
	public function __distruct() {
		$this->db->dbDisconnect ();
	}
	
	/* 获取用户帐号 */
	private function GetUserName() {
		$sql = "select user_name from hs_hr_users  where id ='{$_SESSION['user']}' ";
		$query = $this->db->executeQuery ( $sql );
		$row = mysql_fetch_array ( $query, MYSQL_ASSOC );
		if (is_array ( $row )) {
			$this->UserName = $row ['user_name'];
		} else {
			$this->_redirect( " 读取用户信息出错啦");
			exit;
		}
		
	}

	/* 获取考勤管理权限（新版）2013-07-22
	 * 先从hs_hr_workgroup获取权限
	 *  */
	private function GetPermissions() {
		
		$l_sql = "SELECT count(*) as l FROM `hs_hr_workgroup` WHERE `group_leader` = '{$this->UserName}'";
		$l_query = $this->db->executeQuery ( $l_sql );
		$l_row = mysql_fetch_array ( $l_query, MYSQL_ASSOC );
		$l_reslut = $l_row ['l'];
		
		$m_sql = "SELECT count(*) as m FROM `hs_hr_workgroup` WHERE `manager` ='{$this->UserName}'";
		$m_query = $this->db->executeQuery ( $m_sql );
		$m_row = mysql_fetch_array ( $m_query, MYSQL_ASSOC );
		$m_reslut = $m_row ['m'];
		
		
		$b_sql = "SELECT count(*) as b FROM `hs_hr_workgroup` WHERE `boss` = '{$this->UserName}'";
		$b_query = $this->db->executeQuery ( $b_sql );
		$b_row = mysql_fetch_array ( $b_query, MYSQL_ASSOC );
		$b_reslut = $b_row ['b'];
		
		return $permission = $l_reslut+$m_reslut+$b_reslut;
	
	}

	
	/*
	 * 读取审批数据(新版)
	 * 2013-07-22
	 * @param int $permission > 0
	 * return array
	 */
	public function ShowList($TypeId = 0) {
		$permission = $this->GetPermissions ();

		if($permission > 0){
			$where = " (c.group_leader = '{$this->UserName}' and  c.IsCheck = 0 ) or 
			(c.manager ='{$this->UserName}'  and  c.IsCheck = 3 ) or 
			(c.boss = '{$this->UserName}'  and  c.IsCheck = 4 ) ";
		}else {
			$this->_redirect ( " 你没有OA审批权限");
			exit;
		}
		
		if ($TypeId == 0) {
			$where .= " and c.TypeId in (1,2,4,5,6)";
			$title = "请假";
		} else if($TypeId == 9){
			$where .= " and c.TypeId in (9,10,11)";
			$title = "加班";
		}else {
			$where .= " and c.TypeId = {$TypeId}";
			$title = $this->AttendanceCache ['SpecialAdType'] [$TypeId];
		}
	
		$sysConst = new sysConf ();
		$pageNO = isset ( $_POST['pageNO'] ) ? intval ( $_POST['pageNO'] ) : 1;
		if ($pageNO > 0) {
			$pageNO --;
			$pageNO *= $sysConst->itemsPerPage;
				
			$limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
		}
	
		$sql = "select count(*) as allcount  from hs_hr_workgroup as a left join hs_hr_employee_workgroup as b " .
				"on a.group_id = b.group_id inner join hs_hr_emp_attendance_sp as c on c.EmpId = b.emp_number " . 
				"left join  hs_hr_users as d on c.EmpId = d.id  left join hs_hr_employee as e on c.EmpId = e.emp_number  where ".$where;
	
		$query = $this->db->executeQuery ( $sql );
		$Allcount = mysql_fetch_array ( $query, MYSQL_ASSOC );
	
		$reslut = array ();
		$_sql = "select a.name,a.group_type,a.group_leader as leaders,c.IsCheck,c.group_leader,c.manager,c.boss,c.Id,c.TypeId,c.StartTime,c.Content,c.EndTime,c.hour,d.user_name,e.emp_firstname,e.emp_lastname".
				" from hs_hr_workgroup as a left join hs_hr_employee_workgroup as b " .
				"on a.group_id = b.group_id inner join hs_hr_emp_attendance_sp as c on c.EmpId = b.emp_number " . 
				"left join  hs_hr_users as d on c.EmpId = d.id  left join hs_hr_employee as e on c.EmpId = e.emp_number where ".$where." order by Id desc ".$limit;
	
		
		$_query = $this->db->executeQuery ( $_sql );
		while ( $row = mysql_fetch_array ( $_query, MYSQL_ASSOC ) ) {
			// when the manger is leader
			if (strtolower($row ['boss']) == strtolower ( $this->UserName )){
				$row ['group'] = 3;
			}else if (strtolower($row ['manager']) == strtolower ( $this->UserName )) {
				$row ['group'] = 2;
			}else if (strtolower($row ['group_leader']) == strtolower ( $this->UserName )) {
				$row ['group'] = 1;
			} 
				
			$row ['TypeName'] = $this->AttendanceCache ['SpecialAdType'] [$row ['TypeId']];
			$reslut [] = $row;
		}
	
		$records = array ();
		$records['list'] = $reslut;
		$records['title'] = $title;
		$records['pageNo'] = $pageNO;
		$records['Allcount'] = $Allcount ['allcount'];
		$records['TypeId'] = $TypeId;
	
		$path = "/templates/oa/Show_Approval.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
	}

	
	/*
	 * 读取审批纪录(新版)
	* 2013-07-22
	* @param int $permission > 0
	* return array
	*/
	public function ShowLogList($TypeId = 0) {
		$permission = $this->GetPermissions ();
	
		if($permission > 0){
			$where = " (c.group_leader = '{$this->UserName}' or c.manager ='{$this->UserName}' or c.boss = '{$this->UserName}'  ) ";
		}else {
			$this->_redirect ( " 你没有OA审批权限");
			exit;
		}
	
		if ($TypeId == 0) {
			$where .= " and c.TypeId in (1,2,4,5,6)";
			$title = "请假";
		} else if($TypeId == 9){
			$where .= " and c.TypeId in (9,10,11)";
			$title = "加班";
		}else {
			$where .= " and c.TypeId = {$TypeId}";
			$title = $this->AttendanceCache ['SpecialAdType'] [$TypeId];
		}
	
		$sysConst = new sysConf ();
		$pageNO = isset ( $_POST['pageNO'] ) ? intval ( $_POST['pageNO'] ) : 1;
		if ($pageNO > 0) {
			$pageNO --;
			$pageNO *= $sysConst->itemsPerPage;
	
			$limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
		}
	
		$sql = "select count(*) as allcount  from hs_hr_workgroup as a left join hs_hr_employee_workgroup as b " .
				"on a.group_id = b.group_id inner join hs_hr_emp_attendance_sp as c on c.EmpId = b.emp_number " .
				"left join  hs_hr_users as d on c.EmpId = d.id  left join hs_hr_employee as e on c.EmpId = e.emp_number  where ".$where;
	
		$query = $this->db->executeQuery ( $sql );
		$Allcount = mysql_fetch_array ( $query, MYSQL_ASSOC );
	
		$reslut = array ();
		$_sql = "select a.name,a.group_type,a.group_leader as leaders,c.IsCheck,c.group_leader,c.manager,c.boss,c.Id,c.TypeId,c.StartTime,c.Content,c.EndTime,c.hour,d.user_name,e.emp_firstname,e.emp_lastname".
				" from hs_hr_workgroup as a left join hs_hr_employee_workgroup as b " .
				"on a.group_id = b.group_id inner join hs_hr_emp_attendance_sp as c on c.EmpId = b.emp_number " .
				"left join  hs_hr_users as d on c.EmpId = d.id  left join hs_hr_employee as e on c.EmpId = e.emp_number where ".$where." order by Id desc ".$limit;
	
	
		$_query = $this->db->executeQuery ( $_sql );
		while ( $row = mysql_fetch_array ( $_query, MYSQL_ASSOC ) ) {
	
			$row ['TypeName'] = $this->AttendanceCache ['SpecialAdType'] [$row ['TypeId']];
			$reslut [] = $row;
		}
	
		$records = array ();
		$records['list'] = $reslut;
		$records['title'] = $title;
		$records['pageNo'] = $pageNO;
		$records['Allcount'] = $Allcount ['allcount'];
		$records['TypeId'] = $TypeId;
	
		$path = "/templates/oa/Show_Approval_log.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
	}
	
	
	/*
	 * 删除特殊考勤的数据
	 * */
	public function deleteSpecialAttend($Id=0,$TypeId){
		if (in_array($TypeId,array(1,2,4,5,6))) {
			$TypeId = 0;
		} else if(in_array($TypeId,array(9,10,11))){
			$TypeId = 9;
		}
		
		$url = "?oacode=oamange&action=attend&TypeId={$TypeId}";
		
		if(empty($Id)){
			$this->redirect("缺少id参数", $url);
		}

		$sql = "update hs_hr_emp_attendance_sp set IsCheck = 2 where Id='{$Id}'";
		if($this->db->executeQuery($sql)) {
			$this->redirect("不审批成功", $url);
		} else {
			$this->redirect("不审批失败", $url);
		}
	}
	
	/**
	 *删除OA自定义数据
	 **/
	public function deleteDefattend($Id=0){
		$url = "?oacode=oamange&action=def";
		if(empty($Id)){
			$this->redirect("缺少id参数", $url);
		}
		
		$sql = "update hs_hr_oa_approval set IsCheck = 2 where Id='{$Id}'";
		if($this->db->executeQuery($sql)) {
			$this->redirect("删除成功", $url);
		} else {
			$this->redirect("删除失败", $url);
		}
		
	}
	
	
	/*
	 * to do list 
	 * 返回当前用户所未有审批总数 return total
	 */
	public function ShowRemind() {
		$records = array();
		
		$records = $this->toDoList();
		$records['argclass'] = $this->viewClassArg();
		$path = "/templates/oa/todolist.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
	}
	
	/*
	 * 提醒板块，读取审批数据,登录后提示
	 * */
	public function toDoList(){
		$permission = $this->GetPermissions ();
		if($permission > 0){
			$where = " (group_leader = '{$this->UserName}' and  IsCheck = 0 ) or 
			(manager ='{$this->UserName}'  and  IsCheck = 3 ) or 
			(boss = '{$this->UserName}'  and  IsCheck = 4 ) ";
		}else {
			$this->_redirect ( " 你没有OA审批权限");
			exit;
		}
		
		$sql = "select TypeId,IsCheck,group_leader,manager,boss from hs_hr_emp_attendance_sp where " . $where;
		$reslut = array();
		$query = $this->db->executeQuery ( $sql );
		while($row = mysql_fetch_array ( $query, MYSQL_ASSOC )){
			if (strtolower($row ['group_leader']) == strtolower ( $this->UserName ) && $row['IsCheck'] == 3) {
				continue;
			}else if (strtolower($row ['manager']) == strtolower ( $this->UserName ) && $row['IsCheck'] == 4) {
				continue;
			}else if (strtolower($row ['boss']) == strtolower ( $this->UserName ) && $row['IsCheck'] == 1){
				continue;
			}else if ($row['IsCheck'] == 2){
				continue;
			}
			$reslut [] = $row ['TypeId'];
		}

		$count_keys = array_count_values($reslut);
		//以下是的单独取出请假和加班的数据合成
		$_reslut[0] = array('total'=>($count_keys[1]+$count_keys[2]+$count_keys[4]+$count_keys[5]+$count_keys[6]),'name'=>'请假','TypeId'=>0);
		$_reslut[9] = array('total'=>($count_keys[9]+$count_keys[10]+$count_keys[11]),'name'=>'加班','TypeId'=>9);
		
		foreach($this->AttendanceCache ['SpecialAdType'] as $key=>$v){
			if($key > 6 && !in_array($key,array(9,10,11))){
				$_reslut[($key-6)] = array('total'=>$count_keys[$key],'name'=>$v,'TypeId'=>$key);
			}
			
			if($key == 3){
				$_reslut[3] = array('total'=>$count_keys[3],'name'=>$v,'TypeId'=>$key);
			}
		}
		
		//判断自定义OA数据
		$total = array();
		$d_sql = "select count(*) as total from hs_hr_oa_approval  where " . $where;
		$_query = $this->db->executeQuery ( $d_sql );
		$total = mysql_fetch_array ( $_query, MYSQL_ASSOC );
		
		
		//员工合同转正或者到期提醒
		$emsql = "select count(*) as total from  hs_hr_employee as b inner join `hs_hr_emp_contract_extend` as c on c.emp_number = b.emp_number where TIMESTAMPDIFF(day,'".@date("Y-m-d")."',econ_extend_end_date) < 4 and c.econ_type < 4";
		$emquery = $this->db->executeQuery ( $emsql );
		$em_total = mysql_fetch_array ( $emquery, MYSQL_ASSOC );
		
		$records['total'] = $total['total'];
		$records['list'] = $_reslut;
		$records['hetong'] = $em_total['total'];
		
		return $records;
	}
	
	/*
	 * 读取本月排班数据
	**/
	public  function viewClassArg() {
		$records = array();
		$year = @date('Y',time());
		$month = @date('n',time());
		$sql = "select * from hs_hr_argclass_year where c_year='$year' and c_month = '$month'";
		$query = $this->db->executeQuery($sql);
		$list = array();
		while ($row = mysql_fetch_array($query, MYSQL_ASSOC)) {
			$group = trim($row['c_group'],'班');
			$month = $row['c_month'];
			$day = $row['c_day'];
			$list[$group][$month][$day] = $row['c_class'];
		}
		$records['list'] = $list;
		$records['year'] = $year;
		$records['rights'] = $_SESSION['localRights'];
		return $records;
	}
	
	/*
	**读取快要到期的合同员工
	*/
	public function showContract(){

		$sysConst = new sysConf ();
		$pageNO = isset ( $_POST['pageNO'] ) ? intval ( $_POST['pageNO'] ) : 1;
		if ($pageNO > 0) {
			$pageNO --;
			$pageNO *= $sysConst->itemsPerPage;
			
			$limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
		}
		
		$csql = "select count(*) as allcount from  hs_hr_employee as b inner join `hs_hr_emp_contract_extend` as c on c.emp_number = b.emp_number where TIMESTAMPDIFF(day,'".@date("Y-m-d")."',econ_extend_end_date) < 4 and c.econ_type < 4";
		$cquery = $this->db->executeQuery ( $csql );
		$Allcount = mysql_fetch_array ( $cquery, MYSQL_ASSOC );
		
		$_reslut =array();
		$emsql = "select b.employee_id,b.emp_lastname,b.emp_firstname,c.econ_extend_start_date,c.econ_extend_end_date,c.econ_type as total from  hs_hr_employee as b inner join `hs_hr_emp_contract_extend` as c on c.emp_number = b.emp_number where TIMESTAMPDIFF(day,'".@date("Y-m-d")."',c.econ_extend_end_date) < 4 and econ_type < 4".$limit;
		$emquery = $this->db->executeQuery ( $emsql );
		while ($row = mysql_fetch_array($emquery, MYSQL_ASSOC)) {
			$_reslut[] = $row;
		}
		
		$records['list'] = $_reslut;
		$records['Allcount'] = $Allcount ['allcount'];
		$path = "/templates/oa/Show_contract.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
	}
	
	/*
	 * 特殊考勤审批
	 * IsCheck 0(未审批) 1审批完成 2是不审批 3是组长审批完成 4是经理审批完成  老板审批完成状态变成1,即为全部完成
	 */
	public function Approval() {
		$TypeId = isset($_POST['TypeId'] ) ?  $_POST['TypeId']  : "";
		
		if($TypeId  !=  ""){
			$url = "&action=attend&TypeId=".$TypeId;
		}else{
			$url = "";
		}
		
		$leaders = $_POST[leaders];
		$managers = $_POST[managers];
		$boss = $_POST[boss];
		
		$permission = $this->GetPermissions ();
		if($permission > 0){
			if (isset ( $_POST[leaders] ) && count ( $_POST[leaders] ) > 0) {
				foreach ( $_POST[leaders] as $k ) {
					$l_sql = "UPDATE  `hs_hr_emp_attendance_sp`  SET  `IsCheck` = '3'  WHERE `Id` = '$k' ";
					$this->db->executeQuery ( $l_sql );
				}
			}
			
			if (isset ( $_POST[managers] ) && count ( $_POST[managers] ) > 0) {
				foreach ( $_POST[managers] as $j ) {
					$m_sql = "UPDATE  `hs_hr_emp_attendance_sp`  SET  `IsCheck` = '4'  WHERE `Id` = '$j' ";
					$this->db->executeQuery ( $m_sql );
				}
			}
			
			if (isset ( $_POST[boss] ) && count ( $_POST[boss] ) > 0) {
				foreach ( $_POST[boss] as $b ) {
					$b_sql = "UPDATE  `hs_hr_emp_attendance_sp`  SET  `IsCheck` = '1'  WHERE `Id` = '$b' ";
					$this->db->executeQuery ( $b_sql );
					
					//当前类型为年假并且为最高级审核是执行扣除年假
					if($TypeId==3){	
						$csql = "select EmpId,IsCheck,hour from hs_hr_emp_attendance_sp WHERE `Id` = '$b'";
						$cquery = $this->db->executeQuery ( $csql );
						$reslut = mysql_fetch_array ( $cquery, MYSQL_ASSOC );
						if(is_array($reslut) && $reslut['IsCheck'] == 1){
							$hour = $reslut['hour'];
							$this->db->executeQuery ("update hs_hr_emp_annualleave set ResidualHours = ResidualHours - $hour where EmpId = '".$reslut['EmpId']."'");
						}	
					}
					
				}
				
				
			}
			
			$this->redirect ("审批成功", "?oacode=oamange" .$url);
			exit;
		} else {
			$this->_redirect ( " 你没有OA审批权限");
			exit;
		}

	}
	
	/*
	 *自定义OA菜单
	 *
	 */
	public function addOaMenu($Id=""){
		$records = array();
		$user_sql = "select user_name,first_name from hs_hr_users order by id asc ";
		$user_query = $this->db->executeQuery ( $user_sql );
		while ( $row = mysql_fetch_array ( $user_query, MYSQL_ASSOC ) ) {
			$reslut[] = $row;
		}
		$records['user'] = $reslut;
		if($Id > 0){
			$sql = "select mid,typename,description,mood1,mood2 from hs_hr_oamenu where mid={$Id}";
			$query = $this->db->executeQuery($sql);
	
			 $_result = mysql_fetch_array($query, MYSQL_ASSOC);
			 $records['row'] = $_result;
		}
		
		$path = "/templates/oa/AddOaMenu.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
		
	}
	
	
	/*
	 *显示自定义菜单
	 */
	public function showOaMenu(){
		$sysConst = new sysConf ();
		$pageNO = isset ( $_POST['pageNO'] ) ? intval ( $_POST['pageNO'] ) : 1;
		if ($pageNO > 0) {
			$pageNO --;
			$pageNO *= $sysConst->itemsPerPage;
			
			$limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
		}
		
		$csql = "select count(*) as allcount from hs_hr_oamenu";
		$cquery = $this->db->executeQuery ( $csql );
		$Allcount = mysql_fetch_array ( $cquery, MYSQL_ASSOC );
		
		$reslut = array();
		$sql = "select * from hs_hr_oamenu order by mid desc ".$limit;
		$query = $this->db->executeQuery ( $sql );
		while ( $row = mysql_fetch_array ( $query, MYSQL_ASSOC ) ) {
			$reslut[] = $row;
		}
		
		$records = array ();
		$records['list'] = $reslut;
		$records['pageNo'] = $pageNO;
		$records['Allcount'] = $Allcount ['allcount'];
		
		$path = "/templates/oa/Show_OaMenu.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
	}
	
	/**
	 *保存自定义OA菜单
	 **/
	public function SaveOaMenu() {
		
		$mid = intval($_POST["mid"]);
		$typename = $_POST["typename"];
		$description = $_POST["description"];
		$mood1 = $_POST["mood1"];
		$mood2 = $_POST["mood2"];
		
		if($mid > 0) {
			$sql = "update hs_hr_oamenu set typename = '{$typename}',description = '{$description}',mood1 = '{$mood1}',mood2 = '{$mood2}'  where mid='{$mid}'";
			if($this->db->executeQuery($sql)) {
				$this->redirect("保存成功", "?oacode=oamange&action=show");
			} else {
				$this->redirect("保存失败", "?oacode=oamange&action=addoamenu&Id=".$mid);
			}
		} else {
			$sql = "INSERT INTO `hs_hr_oamenu` (`typename` ,`description`,`mood1`,`mood2`)VALUES ('$typename','$description','$mood1','$mood2')";
			if($this->db->executeQuery($sql)) {
				$this->redirect("添加成功", "?oacode=oamange&action=show");
			} else {
				$this->redirect("添加失败", "?oacode=oamange&action=addoamenu");
			}
		}
		
	}

	/**
	 *删除自定义菜单 
	 **/
	public function delOaMenu() {
		$mids = implode(',',$_POST["mid"]);
		$url = "?oacode=oamange&action=show";
		if(!empty($mids)){
		$sql = "delete from hs_hr_oamenu where mid in ({$mids})";
			if($this->db->executeQuery($sql)) {
				$this->redirect("删除成功", $url);
			} else {
				$this->redirect("删除失败", $url);
			}
		}else{
			$this->redirect("无效删除", $url);
		}
		
	}
	
	/*
	 *获取自定义OA菜单
	 **/
	public function getAllMenus(){
		$reslut = array();
		$sql = "select mid,typename from hs_hr_oamenu";
		$query = $this->db->executeQuery ( $sql );
		while ( $row = mysql_fetch_array ( $query, MYSQL_ASSOC ) ) {
			$mid = $row['mid'];
			$reslut[$mid] = $row['typename'];
		}
		
		return $reslut;
	}
	
	
	/*
	 * 读取自定义oa审批数据
	 * @param return array
	 */
	public function ShowListDef() {
		$permission = $this->GetPermissions();
		if($permission > 0){
			$where = " (c.group_leader = '{$this->UserName}' and  c.IsCheck = 0 ) or 
			(c.manager ='{$this->UserName}'  and  c.IsCheck = 3 ) or 
			(c.boss = '{$this->UserName}'  and  c.IsCheck = 4 ) ";
		}else {
			$this->_redirect ( " 你没有OA审批权限");
			exit;
		}
	
		$sysConst = new sysConf ();
		$pageNO = isset ( $_POST['pageNO'] ) ? intval ( $_POST['pageNO'] ) : 1;
		if ($pageNO > 0) {
			$pageNO --;
			$pageNO *= $sysConst->itemsPerPage;
			$limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
		}
		
		$defMenu = $this->getAllMenus();//获取自定义菜单
		
		$sql = "select count(*) as allcount  from hs_hr_workgroup as a left join hs_hr_employee_workgroup as b " .
				"on a.group_id = b.group_id inner join hs_hr_oa_approval as c on c.EmpId = b.emp_number " . 
				"left join  hs_hr_users as d on c.EmpId = d.id  left join hs_hr_employee as e on c.EmpId = e.emp_number  where ".$where;
	
		$query = $this->db->executeQuery ( $sql );
		$Allcount = mysql_fetch_array ( $query, MYSQL_ASSOC );
	
		$reslut = array ();
		$_sql = "select a.name,a.group_type,a.group_leader as leaders,c.IsCheck,c.group_leader,c.manager,c.boss,c.Id,c.TypeId,c.StartTime,c.Content,c.EndTime,c.hour,d.user_name,e.emp_firstname,e.emp_lastname".
				" from hs_hr_workgroup as a left join hs_hr_employee_workgroup as b " .
				"on a.group_id = b.group_id inner join hs_hr_oa_approval as c on c.EmpId = b.emp_number " . 
				"left join  hs_hr_users as d on c.EmpId = d.id  left join hs_hr_employee as e on c.EmpId = e.emp_number where ".$where." order by Id desc ".$limit;
	
		
		$_query = $this->db->executeQuery ( $_sql );
		while ( $row = mysql_fetch_array ( $_query, MYSQL_ASSOC ) ) {
			// when the manger is leader
			if (strtolower($row ['boss']) == strtolower ( $this->UserName )){
				$row ['group'] = 3;
			}else if (strtolower($row ['manager']) == strtolower ( $this->UserName )) {
				$row ['group'] = 2;
			}else if (strtolower($row ['group_leader']) == strtolower ( $this->UserName )) {
				$row ['group'] = 1;
			} 
			
			$row ['TypeId'] = $defMenu[$row ['TypeId']];
			$reslut [] = $row;
		}
		
		$records = array ();
		$records['list'] = $reslut;
		$records['title'] = $title;
		$records['pageNo'] = $pageNO;
		$records['Allcount'] = $Allcount ['allcount'];
		$records['TypeId'] = $TypeId;
		
		$path = "/templates/oa/Show_defApproval.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
	}
	
	
	/*
	 * 读取自定义oa审批纪录
	* @param return array
	*/
	public function ShowLogListDef() {
		$permission = $this->GetPermissions();
		if($permission > 0){
			$where = " (c.group_leader = '{$this->UserName}' or c.manager ='{$this->UserName}' or c.boss = '{$this->UserName}' ) ";
		}else {
			$this->_redirect ( " 你没有OA审批权限");
			exit;
		}
	
		$sysConst = new sysConf ();
		$pageNO = isset ( $_POST['pageNO'] ) ? intval ( $_POST['pageNO'] ) : 1;
		if ($pageNO > 0) {
			$pageNO --;
			$pageNO *= $sysConst->itemsPerPage;
			$limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
		}
	
		$defMenu = $this->getAllMenus();//获取自定义菜单
	
		$sql = "select count(*) as allcount  from hs_hr_workgroup as a left join hs_hr_employee_workgroup as b " .
				"on a.group_id = b.group_id inner join hs_hr_oa_approval as c on c.EmpId = b.emp_number " .
				"left join  hs_hr_users as d on c.EmpId = d.id  left join hs_hr_employee as e on c.EmpId = e.emp_number  where ".$where;
	
		$query = $this->db->executeQuery ( $sql );
		$Allcount = mysql_fetch_array ( $query, MYSQL_ASSOC );
	
		$reslut = array ();
		$_sql = "select a.name,a.group_type,a.group_leader as leaders,c.IsCheck,c.group_leader,c.manager,c.boss,c.Id,c.TypeId,c.StartTime,c.Content,c.EndTime,c.hour,d.user_name,e.emp_firstname,e.emp_lastname".
				" from hs_hr_workgroup as a left join hs_hr_employee_workgroup as b " .
				"on a.group_id = b.group_id inner join hs_hr_oa_approval as c on c.EmpId = b.emp_number " .
				"left join  hs_hr_users as d on c.EmpId = d.id  left join hs_hr_employee as e on c.EmpId = e.emp_number where ".$where." order by Id desc ".$limit;
	
	
		$_query = $this->db->executeQuery ( $_sql );
		while ( $row = mysql_fetch_array ( $_query, MYSQL_ASSOC ) ) {
			$row ['TypeId'] = $defMenu[$row ['TypeId']];
			$reslut [] = $row;
		}
	
		$records = array ();
		$records['list'] = $reslut;
		$records['title'] = $title;
		$records['pageNo'] = $pageNO;
		$records['Allcount'] = $Allcount ['allcount'];
		$records['TypeId'] = $TypeId;
	
		$path = "/templates/oa/Show_defApproval_log.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
	}
	
	/*
	 * 自定义审批
	 */
	public function defApproval() {
		$url = "&action=def";
		
		$leaders = $_POST[leaders];
		$managers = $_POST[managers];
		$boss = $_POST[boss];
		
		$permission = $this->GetPermissions ();
		if($permission > 0){
			if (isset ( $_POST[leaders] ) && count ( $_POST[leaders] ) > 0) {
				foreach ( $_POST[leaders] as $k ) {
					$l_sql = "UPDATE  `hs_hr_oa_approval`  SET  `IsCheck` = '3'  WHERE `Id` = '$k' ";
					$this->db->executeQuery ( $l_sql );
				}
			}
			
			if (isset ( $_POST[managers] ) && count ( $_POST[managers] ) > 0) {
				foreach ( $_POST[managers] as $j ) {
					$m_sql = "UPDATE  `hs_hr_oa_approval`  SET  `IsCheck` = '4'  WHERE `Id` = '$j' ";
					$this->db->executeQuery ( $m_sql );
				}
			}
			
			if (isset ( $_POST[boss] ) && count ( $_POST[boss] ) > 0) {
				foreach ( $_POST[boss] as $b ) {
					$b_sql = "UPDATE  `hs_hr_oa_approval`  SET  `IsCheck` = '1'  WHERE `Id` = '$b' ";
					$this->db->executeQuery ( $b_sql );
				}
				
				
			}
			
			$this->redirect ("审批成功", "?oacode=oamange" .$url);
			exit;
		} else {
			$this->_redirect ( " 你没有OA审批权限");
			exit;
		}
	}
	
	
	
	/**
	 *公告
	 */
	 public function addNews($Id=""){
		$records = array();
		if($Id > 0){
			$sql = "select aid,title,flag,body,writer,pubdate from hs_hr_news where aid={$Id}";
			$query = $this->db->executeQuery($sql);
			$records = mysql_fetch_array($query, MYSQL_ASSOC);
		}

		$path = "/templates/oa/AddNews.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
		
	}
	
	/*
	 *查看公告内容
	 */
 	public function showArticle($Id=""){
		$records = array();
		if($Id > 0){
			$sql = "select aid,title,flag,body,writer,pubdate from hs_hr_news where aid={$Id}";
			$query = $this->db->executeQuery($sql);
			$records = mysql_fetch_array($query, MYSQL_ASSOC);
		}

		$path = "/templates/oa/show_Article.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
		
	} 
	/*
	 *显示公告
	 */
	public function showOaNews(){
		$sysConst = new sysConf ();
		$pageNO = isset ( $_POST['pageNO'] ) ? intval ( $_POST['pageNO'] ) : 1;
		if ($pageNO > 0) {
			$pageNO --;
			$pageNO *= $sysConst->itemsPerPage;
			
			$limit = " limit {$pageNO}, {$sysConst->itemsPerPage}";
		}
		
		$reslut = array();
		$sql = "select * from hs_hr_news order by aid desc ".$limit;
		$query = $this->db->executeQuery ( $sql );
		while ( $row = mysql_fetch_array ( $query, MYSQL_ASSOC ) ) {
			$reslut[] = $row;
		}
		
		$records = array ();
		$records['list'] = $reslut;
		$records['pageNo'] = $pageNO;
		$records['Allcount'] = $Allcount ['allcount'];
		
		$path = "/templates/oa/Show_News.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
	}
	
	/**
	 *保存公告
	 **/
	public function saveOaNews($aid = "") {
		
		$aid = intval($_POST["aid"]);
		$title = $_POST["title"];
		$flag = $_POST["flag"];
		$writer = $_POST["writer"];
		$body = $_POST["content"];
		
		//设置为首页显示时将全部设置为普通
		if($flag == 'a'){
			$usql = "UPDATE hs_hr_news SET flag = 'c'";
			$this->db->executeQuery($usql);
		}
		
		if($aid > 0) {
			
			$sql = "update hs_hr_news set title = '{$title}',flag = '$flag',body = '{$body}' ,writer='$writer' where aid='{$aid}'";
			if($this->db->executeQuery($sql)) {
				$this->redirect("保存成功", "?oacode=oamange&action=news");
			} else {
				$this->redirect("保存失败", "?oacode=oamange&action=news&s=add&Id=".$aid);
			}
		} else {
			$sql = "INSERT INTO `hs_hr_news` (`title`,`flag`,`body`,`writer`,`pubdate`)VALUES ('$title','$flag','$body','$writer','".@date('Y-m-d G:i:s')."')";
			if($this->db->executeQuery($sql)) {
				$this->redirect("添加成功", "?oacode=oamange&action=news");
			} else {
				$this->redirect("添加失败", "?oacode=oamange&action=news");
			}
		}
		
	}
	
	
	/**
	 *删除新闻公告
	 **/
	public function delOaNews() {
		$aids = implode(',',$_POST[delid]);
		
		$url = "?oacode=oamange&action=news";
		if(!empty($aids)){
			$sql = "delete from hs_hr_news where aid in ({$aids})";
			if($this->db->executeQuery($sql)) {
				$this->redirect("删除成功", $url);
			} else {
				$this->redirect("删除失败", $url);
			}
		}else{
			$this->redirect("无效删除", $url);
		}
		
	}
	
	public function _redirect($message){
		$records = $message;
		$path = "/templates/oa/show_message.php";
		$template = new TemplateMerger ( $records, $path );
		$template->display ();
	}
}
?>